#ifndef ES_C24_H_
#define ES_C24_H_


#include "std.h"

//速率功率定义 (Init成员函数使用)

#define NRF24_H_2M 0x0f
#define NRF24_H_1M 0x07
#define NRF24_H_250k 0x27

//end of 速率功率定义 (Init成员函数使用)




#define TX_ADR_WIDTH 	5  	//?????????í??
#define RX_ADR_WIDTH    5

#define CHANAL 40	//???????? 

// SPI(nRF24L01) commands ,	NRF??SPI?ü???ê?¨?????ê??NRF????????????
#define NRF_READ_REG    0x00  // Define read command to register
#define NRF_WRITE_REG   0x20  // Define write command to register
#define RD_RX_PLOAD 0x61  // Define RX payload register address
#define WR_TX_PLOAD 0xA0  // Define TX payload register address
#define FLUSH_TX    0xE1  // Define flush TX register command
#define FLUSH_RX    0xE2  // Define flush RX register command
#define REUSE_TX_PL 0xE3  // Define reuse TX payload register command
#define NOP         0xFF  // Define No Operation, might be used to read status register

// SPI(nRF24L01) registers(addresses) ??NRF24L01 ?à???????÷???????ê?¨??
#define CONFIG      0x00  // 'Config' register address
#define EN_AA       0x01  // 'Enable Auto Acknowledgment' register address
#define EN_RXADDR   0x02  // 'Enabled RX addresses' register address
#define SETUP_AW    0x03  // 'Setup address width' register address
#define SETUP_RETR  0x04  // 'Setup Auto. Retrans' register address
#define RF_CH       0x05  // 'RF channel' register address
#define RF_SETUP    0x06  // 'RF setup' register address
#define STATUS      0x07  // 'Status' register address
#define OBSERVE_TX  0x08  // 'Observe TX' register address
#define CD          0x09  // 'Carrier Detect' register address
#define RX_ADDR_P0  0x0A  // 'RX address pipe0' register address
#define RX_ADDR_P1  0x0B  // 'RX address pipe1' register address
#define RX_ADDR_P2  0x0C  // 'RX address pipe2' register address
#define RX_ADDR_P3  0x0D  // 'RX address pipe3' register address
#define RX_ADDR_P4  0x0E  // 'RX address pipe4' register address
#define RX_ADDR_P5  0x0F  // 'RX address pipe5' register address
#define TX_ADDR     0x10  // 'TX address' register address
#define RX_PW_P0    0x11  // 'RX payload width, pipe0' register address
#define RX_PW_P1    0x12  // 'RX payload width, pipe1' register address
#define RX_PW_P2    0x13  // 'RX payload width, pipe2' register address
#define RX_PW_P3    0x14  // 'RX payload width, pipe3' register address
#define RX_PW_P4    0x15  // 'RX payload width, pipe4' register address
#define RX_PW_P5    0x16  // 'RX payload width, pipe5' register address
#define FIFO_STATUS 0x17  // 'FIFO Status Register' register address

#define MAX_RT      0x10 //????×??ó????????????±ê????
#define TX_DS		0x20 //?????ê??????±ê????	  // 

#define RX_DR		0x40 //??????????????±ê????


#define NRF_CSN_HIGH()      GPIO_WriteHigh(GPIOA, GPIO_PIN_3)
#define NRF_CSN_LOW()       GPIO_WriteLow(GPIOA, GPIO_PIN_3)		        //csn????
#define NRF_CE_HIGH()	    GPIO_WriteHigh(GPIOD, GPIO_PIN_2)
#define NRF_CE_LOW()	    GPIO_WriteLow(GPIOD, GPIO_PIN_2)	
#define NRF_Read_IRQ()		GPIO_ReadInputPin(GPIOD, GPIO_PIN_3)  GPIO_ReadInputDataBit ( GPIOA, GPIO_Pin_0)  //????????

#define R_RX_PL_WID 0x60
#define FEATURE 0x1D
#define DYNPD 0x1C

#define true 1
#define false 0
typedef struct
{
	u8 flag;
	
	u8 package[32];

	u8 length;
}commPackage;

typedef struct
{
	u8 nodeAddr[5];
	u32 deviceModel;
	u16 deviceVersion;
	u32 key;
}NodeInfo;



typedef enum { ACK, NO_ACK }AckType;

typedef enum { SEND_ADDR, NO_ADDR }SendAddr;

extern void new24DataEventHandler(u8, u8*, u8*, u8);


/**************************公共函数*****************************/
commPackage CommCreatePacket(u8* data, int len, u8* myaddr, AckType ack, SendAddr addr, u8 flag);
void C24Init(void);
void C24Tx(u8* addr, commPackage* p);
void C24IRQ(void);


/*****************************SPI*******************************/
extern u8 SpiWriteBuf(u8 reg, u8* buf, u8 len);
extern u8 SpiWriteReg(u8 reg, u8 data);
extern u8 SpiReadReg(u8 reg);
extern u8 SpiReadBuf(u8 reg, u8* buf, u8 len);


/**************************私有函数*****************************/
u8 getBytes(commPackage* p, u8** data);
void setBytes(u8 * data, u8 len, commPackage* cp);

void decode(commPackage* p);



/****************************nrf24**********************************/

/*********需要修改的函数********/
void IRQ_config(void);
void init_rcc(void);
void init_gpio(void);
/******************************/


/*
* power_speed：功率速率，见顶部define
*/
void NRFInit(u8 power_speed);
u8 NRF_Check(void);
u8 NRFtxData(u8* addr, u8* txBuffer, u8 len);
u8 NRFrxData(u8* addr, u8* rxBuffer, u8* len);
u8 readStat(void);
void NRFflushTx(void);
void flushRx(void);
void NRFrx_mode(void);


//private:




u8 rx(u8* buff, u8* len);
u8 tx(u8* buff, u8 len);
void tx_mode(void);

u8 readReg(u8 reg);
u8 writeReg(u8 reg, u8 data);

u8 writeBuffer(u8 reg, u8* buf, u8 len);

u8 readBuffer(u8 reg, u8* buf, u8 len);


void printChar(char* input);

#endif
